<?php
namespace app\admin\controller;
use think\Db;

/**
 * 后台会员
 */
class Sign extends Admin {
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        $data = array(
            'info'  => array(
                'name' => '报名管理',
                'description' => '管理活动报名',
            ),
            'menu' => array(
                array(
                    'name' => '报名列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '导出列表',
                    'url' => url('export' , array("id" => input("id"))),
                ),
            ),
        );
		
		if( input("id") ) {
			$data['menu'] =  array( array(
				'name' => '活动列表',
				'url' => url('activity/index'),
                'icon' => 'list',
			));
		}
		
		return $data;
    }
    /**
     * 列表
     */
    public function index(){
        //筛选条件
        $where = array();

        $groupId = input('status');
        if(!empty($groupId)){
            $where['status'] = $groupId == 2 ? 0 : $groupId;
        }
		
		$activity_id = input('id');
        if(!empty($activity_id)){
            $where['activity_id'] = $activity_id;
        }
		
        //URL参数
        $pageMaps = array();

        $pageMaps['status'] = $groupId;
        //查询数据
        $limit=20;

        $list = model('Sign')->loadList($where,$limit);

        if ($list){
            foreach ($list as $key => $value){
                $list[$key]['phone'] = $value['tel'];
                $userInfo = model('User')->getInfo($value['user_id']);

                if ($userInfo){
                    $list[$key]['user_name'] = $userInfo['nickname'];
                }else{
                    $list[$key]['user_name'] = '';
                }
                $activityInfo = model('Activity')->getInfo($value['activity_id']);
                if ($activityInfo){
                    $list[$key]['activity_title'] = $activityInfo['title'];
                }
            }
        }
        //位置导航
        $breadCrumb = array('活动列表'=>url());
        //模板传值
        $this->assign('breadCrumb',$breadCrumb);
        $this->assign('list',$list);
        $this->assign('_page',$list->render());
        $this->assign('pageMaps',$pageMaps);
        return $this->fetch();
    }
    /**
     * 详情
     */
    public function info(){
        $signId = input('sign_id');

        $model = model('Sign');
        if (input('post.')){
            if ($signId){
                $status=$model->edit();
            }else{
                $status=$model->add();
            }
            if($status!==false){
                return ajaxReturn(200,'操作成功',url('index'));
            }else{
                return ajaxReturn(0,'操作失败');
            }
        }else{
            $types = array(array('type_id'=>1,'name'=>'孩子'),array('type_id'=>2,'name'=>'个人'),array('type_id'=>3,'name'=>'家庭'));
            $cates = array(array('cate_id'=>1,'name'=>'分类1'),array('cate_id'=>2,'name'=>'分类2'),array('cate_id'=>3,'name'=>'分类3'));
            $this->assign('types',$types);
            $this->assign('cates',$cates);
            $this->assign('info', $model->getInfo($signId));
            return $this->fetch();
        }
    }

    /**
     * 删除
     */
    public function del(){
        $signId = input('id');
        if(empty($signId)){
            return ajaxReturn(0,'参数不能为空');
        }
        if(model('Sign')->del($signId)){
            return ajaxReturn(200,'活动删除成功！');
        }else{
            return ajaxReturn(0,'活动删除失败');
        }
    }
    public function export(){
        //筛选条件
        $where = array();

        $groupId = input('status');
        if(!empty($groupId)){
            $where['status'] = $groupId;
        }
		$id = input('id');
        if(!empty($id)){
            $where['activity_id'] = $id;
        }
        //URL参数
        $pageMaps = array();

        $pageMaps['status'] = $groupId;
        //查询数据

        $list = Db::table('ykb_user_sign')->where($where)->select();

        if ($list){
            foreach ($list as $key => $value){
                $list[$key]['phone'] = $value['tel'];
                $userInfo = model('User')->getInfo($value['user_id']);
                if ($userInfo){
                    $list[$key]['user_name'] = $userInfo['nickname'];
                    unset($list[$key]['user_id']);
                }else{
                    $list[$key]['user_name'] = '';
                    unset($list[$key]['user_id']);
                }
                $activityInfo = model('Activity')->getInfo($value['activity_id']);
                if ($activityInfo){
                    $list[$key]['activity_title'] = $activityInfo['title'];
                    unset($list[$key]['activity_id']);
                }
                $list[$key]['status'] = $value['status']==1?'已入场':'未入场';
                unset($list[$key]['sign_id']);
                unset($list[$key]['createtime']);
            }
        }
        $data = array();
        foreach ($list as $k=>$v){
            $data[$k]['wx_name'] = $v['wx_name'];
            $data[$k]['wx_name_small'] = $v['wx_name_small'];
            $data[$k]['phone'] = "'". $v['phone']. "'";
            $data[$k]['activity_title'] = $v['activity_title'];
            $data[$k]['user_name'] = $v['user_name'];
            $data[$k]['sign_time'] = date('Y-m-d',strtotime($v['sign_time']));
            $data[$k]['enter_time'] = $v['status'] == '已入场' ? date('Y-m-d',strtotime($v['enter_time'])) : '';
            $data[$k]['status'] = $v['status'];
        }
        $filename = '报名情况表'; //设置文件名
        $this->export_csv($data,$filename);
    }

    function export_csv($data=array(),$filename='report'){
		vendor("PHPExcel.PHPExcel");
		$excel = new \PHPExcel();  
		$letter = array('A','B','C','D','E','F','G','H');  
		$tableheader = array('报名姓名','所在学校','报名手机','活动名','核销员','报名日期','入场日期','是否入场');
		for($i = 0;$i < count($tableheader);$i++) {  
			$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");  
		}   
		
		for ($i = 2;$i <= count($data) + 1;$i++) {  
			$j = 0;  
			foreach ($data[$i - 2] as $key=>$value) {  
				$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");  
				$j++;  
			}  
		}  
		  
		$write = new \PHPExcel_Writer_Excel5($excel);  
		header("Pragma: public");  
		header("Expires: 0");  
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
		header("Content-Type:application/force-download");  
		header("Content-Type:application/vnd.ms-execl");  
		header("Content-Type:application/octet-stream");  
		header("Content-Type:application/download");;  
		header('Content-Disposition:attachment;filename="'.$filename.date("Y-m-d").'.xls"');  
		header("Content-Transfer-Encoding:binary");  
		$write->save('php://output');  
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/*
		
        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=".$filename.date("Y-m-d").".xls");
        header("Pragma: no-cache");
        header("Expires: 0");
//导出xls 开始
        if (!empty($title)){
            foreach ($title as $k => $v) {
                $title[$k]= $v;
            }
            $title= implode("\t", $title);
            echo "$title\n";
        }
        if (!empty($data)){
            foreach($data as $key=>$val){
                foreach ($val as $ck => $cv) {
                    $data[$key][$ck]= $cv;
                }
                $data[$key]=implode("\t", $data[$key]);
            }
            echo implode("\n",$data);
        }
		*/
    }

}

